<template>
  <div class="app-container">
    <Find :OriginalFilter="{ static: true }" @Launchform="Launchform5" @findClicked="findClicked" :formList="formList" />
    <Find :searchImg="false" :OriginalFilter="{ static: true }" @findClicked="findClicked1" :formList="formList1" />
    <el-card style="margin-top: 10px" shadow="never" :body-style="{ padding: '15px' }">
      <div style="display: flex">
        <div style="width: 25%">
          <span style="font-weight: 600">选择型体</span>
          <Table @currentRow="clicktogetdata2" :isLoading="isLoading1" :tableHeader="tableHeader" :tableData="SelectiveTypeTableDate" />
          <PageModule
            :PageData="{ pageSizeShow: true, buttonS: true, total: totalL }"
            :layout="'prev, pager'"
            @pageChange="pageChangeL"
            @pageSizeChange="pageSizeChangeL"
          />
        </div>

        <div style="margin-left: 30px; width: 25%">
          <span style="font-weight: 600">型体资料</span>
          <Table @currentRow="clicktogetdata1" :isLoading="isLoading2" :tableHeader="tableHeader2" :tableData="TypeDataQueryMethodsTableDate" />
          <PageModule
            :PageData="{ pageSizeShow: true, buttonS: true, total: total }"
            :layout="'prev, pager'"
            @pageChange="pageChange"
            @pageSizeChange="pageSizeChange"
          />
        </div>

        <div style="margin-left: 30px; width: 40%">
          <span style="font-weight: 600">工艺工序</span>
          <Table
            @currentRow="clicktogetdata5"
            @inputChange="inputChange"
            :summaryData="summaryData1"
            summary
            :isLoading="isLoading3"
            :tableHeader="tableHeader3"
            :tableData="TechnologicalProcessTableDate"
          />
        </div>
      </div>

      <!-- <PageModule :PageData="{ pageSizeShow: true }" /> -->
    </el-card>
  </div>
</template>
<script lang="ts">
import { defineComponent, onMounted } from 'vue'
import JMessage from '@/utils/JMessage'
export default defineComponent({
  name: 'ManualUnitPriceFile'
})
</script>
<script lang="ts" setup>
import { i18n, Jzhcn } from '@/i18n'
import {
  ExploitationVariance,
  SelectiveType,
  TypeDataQuery,
  TechnologicalProcess,
  deleteTypeDataQuery,
  CheckCancelCheck,
  fetchDate,
  copyColorPrice,
  CopyTheUnitPriceFromAnotherType
} from '@/api/ControlDepartment/MaterialManagement/ManualUnitPriceFile'
import { DeleteBySerialNumber } from '@/api/ControlDepartment/MaterialManagement/ManualInstructionUnitPrice'
import fileRequest from '@/utils/fileRequest'
import SynchronizationLoginWork from '@/utils/SynchronizationLoginWork'
import { ElMessage, ElMessageBox } from 'element-plus'
import { ref } from 'vue'

const BlankingTypeSingleQueryParms = ref({
  cfmFlag: '',
  styleColor: '',
  styleNo: ''
})

const seqNoParms = ref('')

const clicktogetdata5 = (row: any) => {
  seqNoParms.value = row.seqNo
}

const DeleteBySerialNumberMethods = async () => {
  try {
    const result = await DeleteBySerialNumber(BlankingTypeSingleQueryParms.value, seqNoParms.value)
    if (result.code === 200) {
      TechnologicalProcessMethods()
      seqNoParms.value = ''
      JMessage.success(result.message)
    } else {
      JMessage.error(result.message)
    }
  } catch (error) {
    JMessage.error('删除失败请重试')
  }
}

const handleCloseMateriaLKlc6 = () => {
  if (seqNoParms.value === '') {
    JMessage.warning('请选择需要删除的数据')
  } else {
    ElMessageBox.confirm('确认要删除数据吗?')
      .then(async () => {
        DeleteBySerialNumberMethods()
      })
      .catch(() => {})
  }
}

// 取开发差异
const ExploitationVarianceMehtodsParms = ref({
  cfmFlag: '',
  styleColor: '',
  styleNo: ''
})

const ExploitationVarianceMehtods = async () => {
  try {
    const result = await ExploitationVariance(ExploitationVarianceMehtodsParms.value)
    if (result.code === 200) {
      TypeDataQueryMethods()
      SelectiveTypeMethods()
      JMessage.success(result.message)
    } else {
      JMessage.error(result.message)
    }
  } catch (error) {
    JMessage.error('取开发差异失败')
  }
}

const formList1 = ref([
  {
    type: 'input',
    inputPlaceholder: '源型体',
    inputValue: '',
    inputLabel: ''
  },
  {
    type: 'input',
    inputPlaceholder: '源颜色',
    inputValue: '',
    inputLabel: ''
  },
  {
    type: 'input',
    inputPlaceholder: '目标型体',
    inputValue: '',
    inputLabel: ''
  },
  {
    type: 'input',
    inputPlaceholder: '目标颜色',
    inputValue: '',
    inputLabel: ''
  },
  {
    type: 'button',
    checkboxLabel: '选源型体'
  },
  {
    type: 'button',
    checkboxLabel: '选目标型体'
  },
  {
    type: 'button',
    checkboxLabel: '复制单价到其他配色'
  },
  {
    type: 'button',
    checkboxLabel: '从其他型体复制单价'
  },
  {
    type: 'button',
    checkboxLabel: '删除单价明细建档'
  }
])

const copyColorPriceParms = ref({
  styleNo: '',
  styleColor: '',
  overRide: 0
})

const CopyTheUnitPriceFromAnotherTypeParms = ref({
  styleNo: '',
  styleColor: '',
  newStyleNo: '',
  newStyleColor: '',
  overRide: 0
})

// 从其他型体复制单价
const CopyTheUnitPriceFromAnotherTypeMethods = async () => {
  try {
    const result = await CopyTheUnitPriceFromAnotherType(CopyTheUnitPriceFromAnotherTypeParms.value)
    if (result.code === 200) {
      TypeDataQueryMethods()
      CopyTheUnitPriceFromAnotherTypeParms.value.overRide = 0
      JMessage.success(result.message)
    } else if (result.code === 301) {
      handleCloseMateriaLKlc1(result.message)
    } else {
      JMessage.error(result.message)
    }
  } catch (error) {
    JMessage.error('从其他型体复制单价出错')
  }
}

const handleCloseMateriaLKlc1 = (cod: string) => {
  CopyTheUnitPriceFromAnotherTypeParms.value.overRide = 1
  ElMessageBox.confirm(cod)
    .then(async () => {
      CopyTheUnitPriceFromAnotherTypeMethods()
    })
    .catch(() => {})
}

const copyColorPriceMethods = async () => {
  try {
    const result = await copyColorPrice(copyColorPriceParms.value)
    if (result.code === 200) {
      TypeDataQueryMethods()
      copyColorPriceParms.value.overRide = 0
      JMessage.success(result.message)
    } else if (result.code === 301) {
      handleCloseMateriaLKlc(result.message)
    } else {
      JMessage.error(result.message)
    }
  } catch (error) {
    JMessage.warning('复制单价到其他配色出错')
  }
}

const handleCloseMateriaLKlc = (cod: string) => {
  copyColorPriceParms.value.overRide = 1
  ElMessageBox.confirm(cod)
    .then(async () => {
      copyColorPriceMethods()
    })
    .catch(() => {})
}

// 复制单价到其他配色
const findClicked1 = (row: string) => {
  if (row === '删除单价明细建档') {
    handleCloseMateriaLKlc6()
  }
  if (row === '选源型体') {
    if (copyColorPriceParms.value.styleNo === '') {
      JMessage.warning('请先选择型体资料')
    } else {
      formList1.value[0].inputValue = copyColorPriceParms.value.styleNo
      formList1.value[1].inputValue = copyColorPriceParms.value.styleColor
    }
  }

  if (row === '选目标型体') {
    formList1.value[2].inputValue = copyColorPriceParms.value.styleNo
    formList1.value[3].inputValue = copyColorPriceParms.value.styleColor

    CopyTheUnitPriceFromAnotherTypeParms.value.styleColor = formList1.value[1].inputValue as string
    CopyTheUnitPriceFromAnotherTypeParms.value.styleNo = formList1.value[0].inputValue as string

    CopyTheUnitPriceFromAnotherTypeParms.value.newStyleColor = formList1.value[3].inputValue as string
    CopyTheUnitPriceFromAnotherTypeParms.value.newStyleNo = formList1.value[2].inputValue as string
  }

  if (row === '复制单价到其他配色') {
    if (copyColorPriceParms.value.styleNo === '') {
      JMessage.warning('源型体和源颜色不可为空')
    } else {
      copyColorPriceMethods()
    }
  }

  if (row === '从其他型体复制单价') {
    if (copyColorPriceParms.value.styleNo === '' || CopyTheUnitPriceFromAnotherTypeParms.value.newStyleColor === '') {
      JMessage.warning('源型体和源颜色不可为空并且目标型体和目标颜色也不可为空')
    } else {
      CopyTheUnitPriceFromAnotherTypeMethods()
    }
  }
}

// 取数
const fetchDateParms = ref()

const clicktogetdata2 = (row: any) => {
  fetchDateParms.value = row
}

const fetchDateMethods = async () => {
  const result = await fetchDate(fetchDateParms.value)
  if (result.code === 200) {
    TypeDataQueryMethods()
    fetchDateParms.value = undefined
    JMessage.success(result.message)
  } else {
    JMessage.error(result.message)
  }
}

const summaryData1 = ref<any>([
  {
    index: 4,
    num: 0
  }
])

// 工艺工序
const TechnologicalProcessParms = ref({
  cfmFlag: '',
  styleColor: '',
  styleNo: ''
})

const choose = ref(-1)

const CheckCancelCheckParms = ref({
  cfmFlag: '',
  styleColor: '',
  styleNo: ''
})

const CheckCancelCheckMethods = async () => {
  const result = await CheckCancelCheck(choose.value, CheckCancelCheckParms.value)
  if (result.code === 200) {
    TypeDataQueryMethods()
    CheckCancelCheckParms.value.styleNo = ''
    JMessage.success(result.message)
  } else {
    JMessage.error(result.message)
  }
}

const improtList = ref({
  cfmFlag: '',
  styleColor: '',
  styleNo: ''
})

const findClicked = async (count: string) => {
  if (count === '取开发差异') {
    ExploitationVarianceMehtods()
  }
  if (count === '删除型体资料') {
    handleCloseMateriaLKlc5()
  }
  if (count === '取数') {
    if (fetchDateParms.value === undefined) {
      JMessage.warning('请选择要取数的数据')
    } else {
      fetchDateMethods()
    }
  }
  if (count === '导出') {
    if (improtList.value.styleNo === '') {
      JMessage.warning('请选择需要导出的数据')
    } else {
      const res = await fileRequest.post<any, any>(`/cost/ExportOfProcessSteps`, improtList.value)
      const link = document.createElement('a') // 利用a标签自定义下载文件名
      let blob = new Blob([res.data]) // 创建Blob对象，设置文件类型
      link.style.display = 'none'
      //设置连接
      link.href = window.URL.createObjectURL(blob)
      link.download = `${res.headers['content-disposition'].split('=')[1]}` //自定义文件名字
      document.body.appendChild(link)
      //模拟点击事件
      link.click()
      document.body.removeChild(link)
    }
  }
  if (count === '检核') {
    choose.value = 0
    if (CheckCancelCheckParms.value.styleNo === '') {
      JMessage.warning('请选择要检核的数据')
    } else {
      CheckCancelCheckMethods()
    }
  }
  if (count === '取消检核') {
    choose.value = 1
    if (CheckCancelCheckParms.value.styleNo === '') {
      JMessage.warning('请选择要检核的数据')
    } else {
      CheckCancelCheckMethods()
    }
  }
}

// 型体资料删除
const deleteTypeDataQueryParms = ref({
  cfmFlag: '',
  styleColor: '',
  styleNo: ''
})

const deleteTypeDataQueryMethods = async () => {
  const result = await deleteTypeDataQuery(deleteTypeDataQueryParms.value)
  if (result.code === 200) {
    TypeDataQueryMethods()
    deleteTypeDataQueryParms.value.styleNo = ''
    JMessage.success(result.message)
  } else {
    JMessage.error(result.message)
  }
}

const handleCloseMateriaLKlc5 = () => {
  if (deleteTypeDataQueryParms.value.styleNo === '') {
    JMessage.warning('请选择需要删除的数据')
  } else {
    ElMessageBox.confirm('确认要删除数据吗?')
      .then(async () => {
        deleteTypeDataQueryMethods()
      })
      .catch(() => {})
  }
}

const Launchform5 = (row: any) => {
  SelectiveTypeMethodsParms.value.styleNo = row[0].inputValue
  SelectiveTypeMethods()
  TypeDataQueryParms.value.styleNo = row[3].inputValue
  TypeDataQueryMethods()
}

const clicktogetdata1 = (row: any) => {
  PunchingUnitPriceFileStyle.value = row.cfmFlag
  TechnologicalProcessParms.value.cfmFlag = row.cfmFlag
  TechnologicalProcessParms.value.styleColor = row.styleColor
  TechnologicalProcessParms.value.styleNo = row.styleNo
  TechnologicalProcessMethods()

  copyColorPriceParms.value.styleColor = row.styleColor
  copyColorPriceParms.value.styleNo = row.styleNo

  deleteTypeDataQueryParms.value.cfmFlag = row.cfmFlag
  deleteTypeDataQueryParms.value.styleColor = row.styleColor
  deleteTypeDataQueryParms.value.styleNo = row.styleNo

  CheckCancelCheckParms.value.cfmFlag = row.cfmFlag
  CheckCancelCheckParms.value.styleColor = row.styleColor
  CheckCancelCheckParms.value.styleNo = row.styleNo

  improtList.value.cfmFlag = row.cfmFlag
  improtList.value.styleColor = row.styleColor
  improtList.value.styleNo = row.styleNo

  inputChangeParms.value.styleNoDetail.cfmFlag = row.cfmFlag
  inputChangeParms.value.styleNoDetail.styleColor = row.styleColor
  inputChangeParms.value.styleNoDetail.styleNo = row.styleNo

  BlankingTypeSingleQueryParms.value.cfmFlag = row.cfmFlag
  BlankingTypeSingleQueryParms.value.styleColor = row.styleColor
  BlankingTypeSingleQueryParms.value.styleNo = row.styleNo

  ExploitationVarianceMehtodsParms.value.cfmFlag = row.cfmFlag
  ExploitationVarianceMehtodsParms.value.styleColor = row.styleColor
  ExploitationVarianceMehtodsParms.value.styleNo = row.styleNo
}

const isLoading3 = ref(true)

const TechnologicalProcessTableDate = ref([])

const TechnologicalProcessMethods = async () => {
  isLoading3.value = true
  const result = await TechnologicalProcess(TechnologicalProcessParms.value)
  TechnologicalProcessTableDate.value = result.list.map((item: any) => {
    if (PunchingUnitPriceFileStyle.value !== 'Y') {
      item.disableConfirm = true
      item.change = 'Y'
    }
    return item
  })
  summaryData1.value[0].num = result.count
  isLoading3.value = false
}

// 型体资料查询
const TypeDataQueryParms = ref({
  pageNum: 1,
  pageSize: 40,
  styleNo: ''
})

const isLoading2 = ref(true)

const TypeDataQueryMethodsTableDate = ref([])

const PunchingUnitPriceFileStyle = ref('')

const TypeDataQueryMethods = async () => {
  isLoading2.value = true
  const result = await TypeDataQuery(TypeDataQueryParms.value)

  TypeDataQueryMethodsTableDate.value = result.list.map((item: any) => {
    if (item.cfmFlag === 'Y') {
      item.backgroundColor = '#3FFF38'
    }
    return item
  })
  if (result.list.length !== 0) {
    TechnologicalProcessParms.value.cfmFlag = result.list[0].cfmFlag
    TechnologicalProcessParms.value.styleColor = result.list[0].styleColor
    TechnologicalProcessParms.value.styleNo = result.list[0].styleNo
    PunchingUnitPriceFileStyle.value = result.list[0].cfmFlag
    BlankingTypeSingleQueryParms.value.cfmFlag = result.list[0].cfmFlag
    BlankingTypeSingleQueryParms.value.styleColor = result.list[0].styleColor
    BlankingTypeSingleQueryParms.value.styleNo = result.list[0].styleNo

    inputChangeParms.value.styleNoDetail.cfmFlag = result.list[0].cfmFlag
    inputChangeParms.value.styleNoDetail.styleColor = result.list[0].styleColor
    inputChangeParms.value.styleNoDetail.styleNo = result.list[0].styleNo

    ExploitationVarianceMehtodsParms.value.cfmFlag = result.list[0].cfmFlag
    ExploitationVarianceMehtodsParms.value.styleColor = result.list[0].styleColor
    ExploitationVarianceMehtodsParms.value.styleNo = result.list[0].styleNo

    TechnologicalProcessMethods()
  }
  total.value = result.total
  isLoading2.value = false
}

const total = ref(0)

const pageChange = (val: any) => {
  TypeDataQueryParms.value.pageNum = val
  TypeDataQueryMethods()
}

const pageSizeChange = (val: any) => {
  TypeDataQueryParms.value.pageSize = val
  TypeDataQueryMethods()
}

// 型体查询
const SelectiveTypeTableDate = ref([])

const isLoading1 = ref(true)

const SelectiveTypeMethodsParms = ref({
  pageNum: 1,
  pageSize: 40,
  styleNo: ''
})

const SelectiveTypeMethods = async () => {
  isLoading1.value = true
  const result = await SelectiveType(SelectiveTypeMethodsParms.value)
  SelectiveTypeTableDate.value = result.list

  totalL.value = result.total
  isLoading1.value = false
}

const totalL = ref(0)

const pageChangeL = (val: any) => {
  SelectiveTypeMethodsParms.value.pageNum = val
  SelectiveTypeMethods()
}

const pageSizeChangeL = (val: any) => {
  SelectiveTypeMethodsParms.value.pageSize = val
  SelectiveTypeMethods()
}

onMounted(() => {
  SelectiveTypeMethods()

  TypeDataQueryMethods()
})

// 修改
const inputChangeParms = ref<any>({
  list: [],
  styleNoDetail: {
    cfmFlag: '',
    styleColor: '',
    styleNo: ''
  }
})

const inputChange = async (row: any) => {
  inputChangeParms.value.list = [row].map((item: any) => {
    if (item.noPriceFlag) {
      item.noPriceFlag = 'Y'
    } else {
      item.noPriceFlag = null
    }
    return item
  })
  const result = await SynchronizationLoginWork.put<any, any>(`/cost/updatePcCutPriceDetail0`, inputChangeParms.value)
  if (result.code === 200) {
    TechnologicalProcessMethods()
    inputChangeParms.value.lsit = ''
    JMessage.success(result.message)
  } else {
    JMessage.error(result.message)
  }
}

const formList = ref([
  {
    type: 'input',
    inputPlaceholder: '选择型体查询',
    inputValue: '',
    inputLabel: ''
  },
  {
    type: 'button',
    checkboxLabel: '取数'
  },
  {
    type: 'button',
    checkboxLabel: '删除型体资料'
  },
  {
    type: 'input',
    inputPlaceholder: '型体资料查询',
    inputValue: '',
    inputLabel: ''
  },
  {
    type: 'button',
    checkboxLabel: '检核'
  },
  {
    type: 'button',
    checkboxLabel: '取消检核'
  },

  {
    type: 'button',
    checkboxLabel: '导出'
  },
  {
    type: 'button',
    checkboxLabel: '取开发差异',
    bgColor: '#1890FF'
  }
])

const tableHeader = ref([
  {
    label: '型体',
    prop: 'styleNo'
  },
  {
    label: '颜色代码',
    prop: 'colorCode'
  },
  {
    label: '颜色名称',
    prop: 'styleColor'
  }
])

const tableHeader2 = ref([
  {
    label: '型体',
    prop: 'styleNo',
    backgroundColor: true
  },
  {
    label: '颜色代码',
    prop: 'colorCode'
  },
  {
    label: '颜色名称',
    prop: 'styleColor'
  }
])

const tableHeader3 = ref([
  {
    label: '序号',
    prop: 'seqNo',
    change: true
  },
  {
    label: '类型',
    prop: 'processType',
    change: true
  },
  {
    label: '工艺',
    prop: 'process',
    change: true
  },
  {
    label: '部位',
    prop: 'partName',
    change: true
  },
  {
    label: '单价',
    prop: 'price'
  },
  {
    label: '片数',
    prop: 'pieceNum'
  },
  {
    label: '备注',
    prop: 'remark'
  }
])
</script>

<style lang="less" scoped>
:deep(.el-table .el-table__footer-wrapper) {
  --el-table-row-hover-bg-color: rgb(128, 255, 0);
}
</style>
